﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class EditMember : BasePage
    {
        private ExcuteDao MemberManage = new ExcuteDao();

        /// <summary>
        /// 页面初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.btnSubmit.Visible = false;
                if (!string.IsNullOrEmpty(Request.QueryString["MemberID"]))
                {
                    Session["UrlType"] = "EditMember";
                    Session["Edit_MemberID"] = SpecialCharHandler.HtmlDiscode(Request.QueryString["MemberID"]).Replace("&amp;", "&");

                    this.btnSubmit.Visible = true;
                    SetInitData();

                }
            }
        }

        /// <summary>
        /// 设置初始化页面信息
        /// </summary>
        private void SetInitData()
        {
            //获取用户信息
            DataTable dtMember = MemberManage.Get_MemberByid(Convert.ToInt32(Request.QueryString["MemberID"]));
            if (dtMember.Rows.Count > 0)
            {
                txt_UserName.Text = SpecialCharHandler.HtmlDiscode(dtMember.Rows[0]["M_Name"].ToString());
                txt_Phone.Text = dtMember.Rows[0]["M_Phone"].ToString();
                txt_Email.Text = dtMember.Rows[0]["M_Email"].ToString();
                txt_RealName.Text = dtMember.Rows[0]["M_RealName"].ToString();
                txt_IDCard.Text = dtMember.Rows[0]["M_Idcard"].ToString();
                txt_M_Number.Text = dtMember.Rows[0]["M_Number"].ToString();
                ddlidcardType.SelectedItem.Text = dtMember.Rows[0]["M_IdcardType"].ToString();
                string strStatus = dtMember.Rows[0]["M_Status"].ToString();

                if (strStatus.Equals("True"))
                {
                    ddlStatus.SelectedValue = "1";
                }
                else
                {
                    ddlStatus.SelectedValue = "0";
                }

            }
            //MemberFilter.Member_pwd = dtMember.Rows[0]["Member_Pwd"].ToString();
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //判断是否保存成功
                bool isOk = false;
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }
                //设置需要添加的数据
                MemberCurrent current = new MemberCurrent();
                current.Member_ID = Convert.ToInt32(Request.QueryString["MemberID"]);
                current.M_Name = SpecialCharHandler.HtmlEncode(txt_UserName.Text);
                current.M_Phone = txt_Phone.Text;
                current.M_Email = txt_Email.Text;
                current.M_RealName = txt_RealName.Text;
                current.M_Idcard = txt_IDCard.Text;
                current.M_IdcardType = ddlidcardType.SelectedItem.Text;

                if (!string.IsNullOrEmpty(txt_M_Number.Text))
                {
                    current.M_Number = Convert.ToInt32(txt_M_Number.Text);
                }
                else
                {
                    current.M_Number = 0;
                }

                if (ddlStatus.SelectedValue.Equals("1"))
                {
                    current.M_Status = true;
                }
                else
                {
                    current.M_Status = false;
                }
                //添加用户信息到数据库中
                if (MemberManage.Update_Member(current) == -1)
                {
                    isOk = true;
                }

                if (isOk)
                {
                    ReturnList("MemberList.aspx", "编辑会员信息成功！");
                }
                else
                {
                    ShowMsg("编辑会员信息失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }
        }

        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (ddlidcardType.SelectedValue == "0")
            {
                if (!AppFunction.CheckIDCard(txt_IDCard.Text))
                {
                    ShowMsg("身份证号不正确，请确认！", 1);
                    txt_IDCard.Focus();
                    return false;
                }
            }
            return true;
        }
    }
}